<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="入库时间" prop="warehouseComeTime">
        <el-date-picker clearable
                        v-model="queryParams.warehouseComeTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择入库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="供应商公司" prop="supplierCompany">
        <el-input
          v-model="queryParams.supplierCompany"
          placeholder="请输入供应商公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商负责人" prop="supplierCompanyPrincipal" label-width="96px">
        <el-input
          v-model="queryParams.supplierCompanyPrincipal"
          placeholder="请输入供应商负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经手人" prop="warehouseComeHandler">
        <el-input
          v-model="queryParams.warehouseComeHandler"
          placeholder="请输入经手人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:come:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:come:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="comeList" @selection-change="handleSelectionChange">
<!--      <el-table-column label="入库表id" align="center" prop="warehouseComeId"/>-->

      <el-table-column label="供应商公司" align="center" prop="supplierCompany"/>
      <el-table-column label="供应商负责人" align="center" prop="supplierCompanyPrincipal"/>
      <el-table-column label="供应商负责人电话" align="center" prop="supplierCompanyPrincipalTel"/>
      <el-table-column label="经手人" align="center" prop="warehouseComeHandler"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="入库时间" align="center" prop="warehouseComeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.warehouseComeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:come:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:come:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改入库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="125px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商公司" prop="supplierCompany">
              <el-input v-model="form.supplierCompany" placeholder="请输入供应商公司"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商负责人" prop="supplierCompanyPrincipal">
              <el-input v-model="form.supplierCompanyPrincipal" placeholder="请输入供应商负责人"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商负责人电话" prop="supplierCompanyPrincipalTel">
              <el-input v-model="form.supplierCompanyPrincipalTel" placeholder="请输入供应商负责人电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经手人" prop="warehouseComeHandler">
              <el-input v-model="form.warehouseComeHandler" placeholder="请输入经手人"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库时间" prop="warehouseComeTime">
              <el-date-picker
                v-model="form.warehouseComeTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-button type="primary" style="float: right;" @click="addRow()">添加</el-button>
      <el-form :model="form" ref="detailsList">
        <el-table :data="form.warehouseComeDetailsList" style="width: 100%" ref="tb">
          <el-table-column fixed prop="materialsName" label="物品名称" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialsName" autocomplete="off" size="small"
                        placeholder="请输入物品名称" :rules="detailRules.materialsName"></el-input>
            </template>
          </el-table-column>
          <el-table-column  prop="materialsCategory" label="物品类别" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialsCategory" autocomplete="off" size="small"
                        placeholder="请输入物品类别" :rules="detailRules.materialsCategory"></el-input>
            </template>
          </el-table-column>

          <el-table-column prop="materialsCount" label="物品数量" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialsCount" autocomplete="off" size="small"
                        placeholder="请输入物品数量" :rules="detailRules.materialsCount"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="materialsPrice" label="物品单价" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialsPrice" autocomplete="off" size="small"
                        placeholder="请输入物品单价" :rules="detailRules.materialsPrice"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="materialsUnit" label="物品单位" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialsUnit" autocomplete="off" size="small"
                        placeholder="请输入物品单位" :rules="detailRules.materialsUnit"></el-input>
            </template>
          </el-table-column>

          <el-table-column prop="materialsDescribe" label="物品描述" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialsDescribe" autocomplete="off" size="small"
                        placeholder="请输入物品描述" :rules="detailRules.materialsDescribe"></el-input>
            </template>
          </el-table-column>

          <el-table-column prop="materialsPicture" label="物品图片" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialsPicture" autocomplete="off" size="small"
                        placeholder="请输入物品图片" :rules="detailRules.materialsPicture"></el-input>
            </template>
          </el-table-column>


          <el-table-column prop="materialsBatchNumber" label="批号" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialsBatchNumber" autocomplete="off" size="small"
                        placeholder="请输入批号" :rules="detailRules.materialsBatchNumber"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="materialsBatch" label="批次" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.materialsBatch" autocomplete="off" size="small"
                        placeholder="请输入批次" :rules="detailRules.materialsBatch"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="warehouseComeMaterialsTargetWarehouse" label="目标仓库" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.warehouseComeMaterialsTargetWarehouse" autocomplete="off" size="small"
                        placeholder="请输入目标仓库" :rules="detailRules.warehouseComeMaterialsTargetWarehouse"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="warehouseComeMaterialsTargetStorageLocation" label="目标库位" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.warehouseComeMaterialsTargetStorageLocation" autocomplete="off" size="small"
                        placeholder="请输入目标库位"
                        :rules="detailRules.warehouseComeMaterialsTargetStorageLocation"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button class="btn_del" size="mini" type="text" @click="deleteTimer(scope.$index,scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>

</template>

<script>
  import {listCome, getCome, delCome, addCome, updateCome} from "@/api/warehouseOutIn/come/come";

  export default {
    name: "Come",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 入库表格数据
        comeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          warehouseComeTime: null,
          supplierCompany: null,
          supplierCompanyPrincipal: null,
          supplierCompanyPrincipalTel: null,
          warehouseComeHandler: null,
        },
        // 表单参数
        form: {
          warehouseComeDetailsList: []
        },
        // 表单校验
        rules: {
          warehouseComeTime: [
            {required: true, message: "入库时间不能为空", trigger: "blur"}
          ],
        },
        //入库记录的表单校验
        detailRules: {
          materialsName: [
            {required: true, message: '物品名称不能为空', trigger: 'blur'}
          ],
          materialsCount: [
            {required: true, message: '物品数量不能为空', trigger: 'blur'}
          ],
          materialsPrice: [
            {required: true, message: '物品单价不能为空', trigger: 'blur'}
          ],
          materialsUnit: [
            {required: true, message: '物品单位不能为空', trigger: 'blur'}
          ],
          materialsBatchNumber: [
            {required: true, message: '批号不能为空', trigger: 'blur'}
          ],
          materialsBatch: [
            {required: true, message: '批次不能为空', trigger: 'blur'}
          ],
          warehouseComeMaterialsTargetWarehouse: [
            {required: true, message: '目标仓库不能为空', trigger: 'blur'}
          ],
          warehouseComeMaterialsTargetStorageLocation: [
            {required: true, message: '目标库位不能为空', trigger: 'blur'}
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询入库列表 */
      getList() {
        this.loading = true;
        listCome(this.queryParams).then(response => {
          this.comeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          warehouseComeId: null,
          warehouseComeTime: null,
          supplierCompany: null,
          supplierCompanyPrincipal: null,
          supplierCompanyPrincipalTel: null,
          warehouseComeHandler: null,
          remark: null,
          warehouseComeDetailsList: []
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.warehouseComeId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加入库";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const warehouseComeId = row.warehouseComeId || this.ids
        getCome(warehouseComeId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改入库";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.warehouseComeId != null) {
              updateCome(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCome(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const warehouseComeIds = row.warehouseComeId || this.ids;
        this.$modal.confirm('是否确认删除入库编号为"' + warehouseComeIds + '"的数据项？').then(function () {
          return delCome(warehouseComeIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/come/export', {
          ...this.queryParams
        }, `come_${new Date().getTime()}.xlsx`)
      },
      /** 入库添加商品 */
      addRow() {
        this.form.warehouseComeDetailsList.push({
          materialsName: '',
          materialsCount: '',
          materialsPrice: '',
          materialsUnit: "",
          warehouseComeMaterialsTargetWarehouse: '',
          warehouseComeMaterialsTargetStorageLocation: '',
          materialsBatchNumber: '',
          materialsBatch: ''
        })
      },
      deleteTimer(index, row) {
        this.form.warehouseComeDetailsList.splice(index,1);
      }
    }
  };
</script>
